import axios from "axios";
import * as https from "https";

const $axios = axios.create({
  httpsAgent: new https.Agent({
    rejectUnauthorized: false
  }),
  //json格式
  headers: {
    "Content-Type": "application/json"
  }
});

export const code2Session = async (code: string, appId: string, appSecret: string) => {
  const url = `http://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${appSecret}&js_code=${code}&grant_type=authorization_code`;
  return (await $axios.get(url)).data;
};

export const getAccessToken = async (appId: string, appSecret: string) => {
  const url = "http://api.weixin.qq.com/cgi-bin/token";

  return await $axios.get(url, { params: { grant_type: "client_credential", appid: appId, secret: appSecret } });
};

export const getPhoneNumber = async (code: string, accessToken:string)=>{
  const url = `http://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${accessToken}`;
  return await $axios.post(url, { code });
}
